<template>
  <div class="app-container">
    <el-card class="search-from" ref="searchRef">
      <el-form
        :inline="true"
        class="form-inline"
        :model="searchfilter"
        ref="searchFormRef"
        label-width="70"
      >
        <el-row>
          <el-col :span="18" v-for="(item, index) in formItemList">
            <el-form-item :label="item.label" :prop="item.field" style="width: 50%">
              <el-input v-model="searchfilter[item.field]" style="width: 100%"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :icon="Search">查询</el-button>
              <el-button type="primary" @click="addSchedule" :icon="Plus">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="list">
      <el-table style="width: 100%; height: 100%" :data="dataList">
        <el-table-column
          type="index"
          width="100"
          label="#"
          align="center"
        ></el-table-column>
        <template v-for="(item, index) in columnList">
          <el-table-column
            :min-width="item.width"
            v-if="item.hasOwnProperty('format')"
            :prop="item.field"
            :label="item.label"
          >
            <template #default="scope">
              {{ moment(scope.row.field).format(item.format) }}
            </template>
          </el-table-column>
          <el-table-column v-else-if="item.field == 'status'"
            :min-width="item.width"
           :prop="item.field"
            :label="item.label"
          >
            <template #default="scope">
                <el-tag :type="statusTag(scope.row.status).type">
                  {{ statusTag(scope.row.status).label }}
                </el-tag>
            </template>
          </el-table-column>
          <el-table-column v-else
          :min-width="item.width"
           :prop="item.field"
            :label="item.label"
          ></el-table-column>
        </template>
        <el-table-column label="操作" align="center"  fixed="right">
            <template #default="scope">
              <el-dropdown>
                <span class="el-dropdown-link">
                  操作
                  <el-icon class="el-icon--right">
                    <arrow-down />
                  </el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>日志</el-dropdown-item>

                    <el-dropdown-item
                      v-if="isInPermission('ScheduleTaskManager.ScheduleTask.Update')"
                      >编辑</el-dropdown-item
                    >

                    <el-dropdown-item
                      v-if="isInPermission('ScheduleTaskManager.ScheduleTask.Pause')"
                      >暂停</el-dropdown-item
                    >

                    <el-dropdown-item
                      v-if="isInPermission('ScheduleTaskManager.ScheduleTask.Start')"
                      >启动</el-dropdown-item
                    >

                    <el-dropdown-item
                      v-if="isInPermission('ScheduleTaskManager.ScheduleTask.RunOnce')"
                      >运行</el-dropdown-item
                    >
                    <el-dropdown-item
                      v-if="isInPermission('ScheduleTaskManager.ScheduleTask.Resume')"
                      >恢复</el-dropdown-item
                    >
                    <el-dropdown-item
                      v-if="isInPermission('ScheduleTaskManager.ScheduleTask.Delete')"
                      >删除</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
      </el-table>
    </el-card>
    <addOrUpdateSchedule   v-if="addOrUpdatevisable" 
      @close="(eventData:boolean)=>{addOrUpdatevisable= eventData}"
     />
  </div>
</template>
<script setup lang="ts">
import { Search, Plus, ArrowDown } from "@element-plus/icons-vue";
import moment from "moment";
import addOrUpdateSchedule from "./components/addOrUpdateSchedule.vue";
let addOrUpdatevisable=ref<boolean>(false);
let searchfilter = ref<any>({ keyword: "" });
const formItemList = [
  {
    label: "关键字",
    field: "keyword",
  },
];
let columnList = [
  {
    label: "任务名称",
    field: "title",
    width: 140,
  },
  {
    label: "分组",
    field: "jobGroup",
    width: 140,
  },
  {
    label: "运行类型",
    field: "runLoop",
    width: 140,
  },
  {
    label: "开始时间",
    field: "runLoop",
    width: 140,
    format: "YYYY-MM-DD HH:mm:ss",
  },
  {
    label: "开始时间",
    field: "startDate",
    width: 140,
    format: "YYYY-MM-DD HH:mm:ss",
  },
  {
    label: "上次运行时间",
    field: "lastRunTime",
    width: 140,
    format: "YYYY-MM-DD HH:mm:ss",
  },
  {
    label: "下次次运行时间",
    field: "lastRunTime",
    width: 140,
    format: "YYYY-MM-DD HH:mm:ss",
  },
  {
    label: "运行次数",
    field: "totalRunCount",
    width: 140,
  },
  {
    label: "状态",
    field: "status",
    width: 140,
  },
  {
    label: "备注",
    field: "status",
    width: 150,
  },
];

const statusTag = (status: number) => {
  let result = {
    type: "success",
    label: "运行中",
  };
  switch (status) {
    case 0:
      result = {
        type: "danger",
        label: "已停止",
      };
      break;
    case 1:
      result = {
        type: "success",
        label: "运行中",
      };
      break;
    default:
      result = {
        type: "warning",
        label: "已暂停",
      };
  }
  return  result ;
};

let dataList = ref<any[]>([]);

const addSchedule=()=>{
  addOrUpdatevisable.value=true;
  
}
</script>
<style scoped type="sass"></style>
